package com.cuiweiyou.happypaint.ui;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cuiweiyou.happypaint.R;
import com.cuiweiyou.happypaint.util.ScreenUtil;
import com.cuiweiyou.happypaint.util.ViewUtil;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;
import java.util.Map;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.PictureHolder> {
    Context context;
    int resizeWidth;
    int resizeHeight;
    List<Map<String, String>> picList;
    OnPictureClickListener onPictureClickListener;

    public HistoryAdapter(OnPictureClickListener listener) {
        this.onPictureClickListener = listener;
    }

    @NonNull
    @Override
    public PictureHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();

        int oneDpPx = ScreenUtil.dp2px(context, 1);
        int screenWidth = ScreenUtil.getScreenWidth(context);
        int screenHeight = ScreenUtil.getScreenHeight(context);
        resizeHeight = oneDpPx * 100;
        resizeWidth = screenWidth * resizeHeight / screenHeight;

        View view = View.inflate(context, R.layout.item_history, null);
        return new PictureHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PictureHolder holder, int position) {
        Map<String, String> map = picList.get(position);
        String json = map.get("json");
        String png = map.get("png");
        Picasso.get()//
                .load(new File(png))//
                .resize(resizeWidth, resizeHeight)
                .placeholder(R.mipmap.icon)//
                .error(R.mipmap.icon)//
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return null == picList ? 0 : picList.size();
    }

    public void setActionList(List<Map<String, String>> list) {
        this.picList = list;
        notifyDataSetChanged();
    }

    class PictureHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        ImageView imageView;

        public PictureHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);

            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getLayoutPosition();
            onPictureClickListener.onPictureClick(position);
        }

        @Override
        public boolean onLongClick(View v) {
            int position = getLayoutPosition();
            onPictureClickListener.onPictureLongClick(position);
            return true;
        }
    }

    public interface OnPictureClickListener {
        public void onPictureClick(int index);
        public void onPictureLongClick(int index);
    }
}
